import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
Vue.use(Vuex)

export default new Vuex.Store({
  // 配置vuex的插件
  // key：指定持久化的名字，默认名字是xuex
  // storage:选用一种持久化的方案，默认是本地存储
  plugins: [
    createPersistedState({
      key: 'HEIMAO-TOUTIAO',
      reducer({ tokenObj, myChannels, histories }) {
        return { tokenObj, myChannels, histories }
      }
    })
  ],
  state: {
    tokenObj: {},
    myChannels: [],
    histories: []
  },

  getters: {
    isLogin(state) {
      return !!state.tokenObj.token
    }
  },
  mutations: {
    SET_TOKEN(state, tokenObj) {
      state.tokenObj = tokenObj
    },
    SET_MY_CHANEELS(state, channels) {
      state.myChannels = channels
    },
    // histories:新的搜索历史数组
    SET_HISTORIES(state, histories) {
      state.histories = histories
    }
  },
  actions: {},
  modules: {}
})
